package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.DutyArrangementResult;
import com.neusoft.neuiotms.dcom.model.QDutyArrangementResult;
import com.neusoft.neuiotms.dcom.repository.CustomizedDutyArrangementResultRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedDutyArrangementResultRepositoryImpl extends AbstractRepository
        implements CustomizedDutyArrangementResultRepository {

    @Override
    public List<DutyArrangementResult> index(final Searchable searchable) {
        final QDutyArrangementResult qDutyArrangementResult = QDutyArrangementResult.dutyArrangementResult;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<DutyArrangementResult> query = this.query().selectFrom(qDutyArrangementResult);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<DutyArrangementResult> search(final Pageable pageable, final Searchable searchable) {
        final QDutyArrangementResult qDutyArrangementResult = QDutyArrangementResult.dutyArrangementResult;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<DutyArrangementResult> query = this.query().selectFrom(qDutyArrangementResult);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return DutyArrangementResult.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QDutyArrangementResult qDutyArrangementResult = QDutyArrangementResult.dutyArrangementResult;

        // 值班安排ID
        where.and(super.equalsLong(qDutyArrangementResult.dutyArrangement.id, searchable, "dutyArrangementId"));

        // 日期检索时间段
        where.and(super.afterOrEqualsDate(qDutyArrangementResult.startTime, searchable, "startTime"));
        where.and(super.beforeOrEqualsDate(qDutyArrangementResult.startTime, searchable, "endTime"));

        // startTime <= afterStartTime
        where.and(super.beforeOrEqualsDate(qDutyArrangementResult.startTime, searchable, "afterStartTime"));

        // startTime >= beforeStartTime
        where.and(super.afterOrEqualsDate(qDutyArrangementResult.startTime, searchable, "beforeStartTime"));

        // endTime >=beforeEndTime
        where.and(super.afterOrEqualsDate(qDutyArrangementResult.endTime, searchable, "beforeEndTime"));

        // endTime <=beforeEndTime
        where.and(super.beforeOrEqualsDate(qDutyArrangementResult.endTime, searchable, "afterEndTime"));

        // 等于开始时间
        where.and(super.equalsDate(qDutyArrangementResult.startTime, searchable, "eqStartTime"));
        // 等于结束时间
        where.and(super.equalsDate(qDutyArrangementResult.endTime, searchable, "eqEndTime"));

        // 小组ID
        if (null != searchable.getLongValue("teamGroupId")) {
            where.and(super.equalsLong(qDutyArrangementResult.teamGroup.id, searchable, "teamGroupId"));
        }

        // 是否删除
        where.and(super.equalsBool(qDutyArrangementResult.dutyArrangement.deleteFlag, searchable, "deleteFlag"));

        // 组织机构
        where.and(super.equalsStr(qDutyArrangementResult.dutyArrangement.orgCode, searchable, "orgCode"));

        // in组织机构
        where.and(super.inStrings(qDutyArrangementResult.dutyArrangement.orgCode, searchable, "orgCodes"));

        // 职能
        where.and(
                super.equalsLong(qDutyArrangementResult.teamGroup.functionalGroup.id, searchable, "functionalGroupId"));

        // 班次
        where.and(super.equalsStr(qDutyArrangementResult.classesCode, searchable, "classesCode"));

        // 人员
        where.and(super.equalsStr(qDutyArrangementResult.teamGroup.users.any().code, searchable, "userName"));
        return where;
    }

}
